package com.xiaohong.search.controller;

import com.xiaohong.search.service.SearchSkuService;
import com.xiaohong.search.service.SearchSpuService;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaohong
 * @Description
 * @date 2020/12/13 0013 14:31
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/all")
public class SearchController {

    @Autowired
    private SearchSpuService searchSpuService;
    @Autowired
    private SearchSkuService searchSkuService;

    /**
     * 搜索
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Map search(@RequestParam(required = false) Map<String,String> searchMap) throws IOException {
        // 先搜索sku
        Map<String, Object> skuResult = searchSkuService.search(searchMap);
        Map<String, Object> spuResult = null;
        // 判断结果条数够不够1页
        if ((Long)skuResult.get("totalPages") <= 1){
            // 再搜索spu
             spuResult = searchSpuService.search(searchMap);
        }

        // 两者结果合并
        HashMap<String, Map<String, Object>> map = new HashMap<>();
        map.put("skuResult",skuResult);
        map.put("spuResult",spuResult);
        return map;
    }

}
